package com.songcha.sakura.ui.adapter

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide


import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.sakura.R
import com.songcha.sakura.struct.M3u8LocalFile
import com.songcha.sakura.struct.VideoType
import com.songcha.library_common.ui.view.loading.WaveLoadingView
import java.io.File



class LocalVideoAdapter(val mContext: Context,
                        list:MutableList<M3u8LocalFile.LocalVideo>,
                        val mListener:IOnLocalVideoListener) : BaseQuickAdapter<M3u8LocalFile.LocalVideo,BaseViewHolder>(R.layout.item_local_video,list) {
    interface IOnLocalVideoListener{
        fun onPlayVideo(video:M3u8LocalFile.LocalVideo)
        fun onDeleteVideo(video:M3u8LocalFile.LocalVideo)
    }

    enum class PayLoadLocalVideo{
        NAME,
        FILE_SIZE,
        WAVE_PROGRESS
    }

    init {
        //addChildClickViewIds(R.id.btn_play_video,R.id.btn_delete_video)
    }

    override fun convert(holder: BaseViewHolder, item: M3u8LocalFile.LocalVideo) {
        convert(holder,item, listOf(PayLoadLocalVideo.NAME))
        convert(holder,item, listOf(PayLoadLocalVideo.FILE_SIZE))
        convert(holder,item, listOf(PayLoadLocalVideo.WAVE_PROGRESS))
    }

    override fun convert(holder: BaseViewHolder, item:M3u8LocalFile.LocalVideo,payloads: List<Any>) {
        if(payloads.isEmpty()){
            super.convert(holder, item, payloads)
            return
        }


        holder.getView<LinearLayout>(R.id.ll_parant).setOnClickListener{
            mListener.onPlayVideo(item)
        }
        /*holder.getView<ImageButton>(R.id.iv_del).setOnClickListener{
            mListener.onDeleteVideo(item)
        }*/
        holder.getView<LinearLayout>(R.id.ll_parant).setOnLongClickListener {
            mListener.onDeleteVideo(item)
            true
        }

        val payload=payloads[0]
            when (payload) {
                PayLoadLocalVideo.NAME->{
                    val arr=item.episodeName.split("-")
                    if(arr.size>1){
                        val episode=arr[1]
                        holder.getView<TextView>(R.id.tv_name).text=episode
                    }
                    if(item.type!=VideoType.M3U8){
                        val iv_thumb=holder.getView<ImageView>(R.id.iv_thumb)
                        /*VideoUtil.getVideoFrame(item.absoluteEpisodePath,125)?.let {
                            Glide.with(context).load(it).into(iv_thumb)
                        }*/
                        Glide.with(context).load(Uri.fromFile(File(item.absoluteEpisodePath))).into(iv_thumb)
                    }
                }
                PayLoadLocalVideo.FILE_SIZE->{
                    holder.getView<TextView>(R.id.tv_fileSize).text=(item.fileSize/(1024*1024)).toString()+"m"
                }
                PayLoadLocalVideo.WAVE_PROGRESS->{
                    if(item.progress>0 && item.duration>0){
                        holder.getView<WaveLoadingView>(R.id.wlv).setProgress((item.progress*1.0/item.duration*100).toInt())
                    }
                }
            }




       /* val tvProgress=holder.getView<TextView>(R.id.tv_progress)
        if(item.progress>0){
            tvProgress.visibility= View.VISIBLE
            tvProgress.text=TimeUtil.getTimeByMillSecond(item.progress)
        }else{
            tvProgress.visibility= View.INVISIBLE
        }*/

    }



}